#ifndef DNSDB_H
#define DNSDB_H

#include <QString>
#include <QVector>
#include <QDateTime>
#include <QSqlDatabase>
#include <Packet/packet.h>

QString dnsTypeString( const uint16_t &type );
QString dnsClassString( const uint16_t &type );
class DNSQuery
{
public:
  DNSQuery();
  DNSQuery( const DNSQuery &query );
  ~DNSQuery();
  QString host() const;
  void setHost( const QString &requestor );
  QString query() const;
  void setQuery( const QString &query );
  QString type() const;
  void setType( const QString &type );
  bool dnsSec() const;
  void setDnsSec( const bool &isDNSSec );
  QString dnsclass() const;
  void setClass( const QString &dnsclass );
  QDateTime timestamp() const;
  void setTimestamp( const uint32_t &time );
  void setTimestamp( const QDateTime &time );

private:
  QString host_;
  QString query_;
  QString type_;
  QString class_;
  bool dnsSec_;
  QDateTime timestamp_;
};

QVector<DNSQuery> queryBuilder( const Packet &p );

class DNSResponse
{
public:
  DNSResponse();
  ~DNSResponse();
  DNSResponse( const DNSResponse &response );
  QString host() const;
  void setHost( const QString &requestor );
  DNSQuery query() const;
  void setQuery( const DNSQuery &query );
  QString name() const;
  void setName( const QString &name );
  QString type() const;
  void setType( const QString &type );
  bool dnsSec() const;
  void setDnsSec( const bool &isDNSSec );
  QString record( ) const;
  void setRecord( const QString &data );
  quint32 ttl( ) const;
  void setTTL( const quint32 &ttl );
  QString dnsclass() const;
  void setClass(const QString &dnsclass);
  void setTimestamp( const uint32_t &time );
  void setTimestamp( const QDateTime &time );
  QDateTime timestamp() const;
  //QString answer() const;
  //void setAnswer( const QString &answer );

private:
  QString host_;
  DNSQuery query_;
  QString name_;
  //QString answer_;
  QString type_;
  QString class_;
  QString record_;
  quint32 ttl_;
  bool dnsSec_;
  QDateTime timestamp_;
};

QVector<DNSResponse> responseBuilder( const Packet &p );
QString dnsAnswerString( const std::vector< uint8_t > &data, const uint16_t &type );
QString ipv4AnswerData( const std::vector< uint8_t > & data );
QString stringAnswerData( const std::vector< uint8_t > &data );

class DNSDB
{
public:
  enum dbtype { none = 0, mysql, sqlite, pgsql };
  DNSDB();
  ~DNSDB();
  DNSDB( const DNSDB& db );
  void setDBHost( const QString &hostname );
  void setDB( const QString &dbname );
  void setDBUser( const QString &username );
  void setDBPassword( const QString &password );
  void setDBType( const dbtype& type );
  void setDBType( const QString &type );
  dbtype dbType() const;
  QString dbHost() const;
  QString db() const;
  QString dbUser() const;
  QString dbPassword() const;
  void dbConnect();
  void recordQuery( const DNSQuery &query );
  void recordResponse( const DNSResponse &response );
  void insertPacket( const Packet &p );

private:
  QString findHost(const QString &hosts );
  QString findQuery( const QString &query, const QString &type, const QString &dnsclass );
  QString findRecord( const DNSResponse &response );

  QString dbhostname_;
  QString dbname_;
  QString dbtype_;
  QString dbuser_;
  QString dbpass_;
  QSqlDatabase db_;

};

#endif // DNSDB_H
